<template>
  <div class="edit-popup">
    <popup ref="popupRef" :title="popupTitle" :async="true" width="700px" @confirm="handleSubmit" @close="handleClose">
      <el-form ref="formRef" :model="formData" label-width="100px" :rules="formRules">
        <el-form-item label="院区" prop="cid">
          <el-select class="flex-1" v-model="formData.cid" clearable placeholder="请选择院区">
            <el-option
                v-for="(item, index) in pagerExtend.campus_list"
                :key="index"
                :label="item.name"
                :value="parseInt(item.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" clearable placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="总电费" prop="all_lift_fee">
          <el-input v-model="formData.all_lift_fee" placeholder="请输入总电费"/>
        </el-form-item>
        <el-form-item label="总二次加压费" prop="all_second_pressure_fee">
          <el-input v-model="formData.all_second_pressure_fee" placeholder="请输入总二次加压费"/>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number v-model="formData.sort" :min="1"/>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>

<script lang="ts" setup name="buildingEdit">
import type {FormInstance} from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {apiBuildingAdd, apiBuildingEdit, apiBuildingDetail} from '@/api/building'
import {timeFormat} from '@/utils/util'
import type {PropType} from 'vue'

defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  },
  pagerExtend: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  },
})

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑楼栋' : '新增楼栋'
})

// 表单数据
const formData = reactive({
  id: '',
  cid: '',
  name: '',
  status: 1,
  sort: 10000,
  all_lift_fee: '',
  all_second_pressure_fee: '',
  is_month_begin: '',
})

// 表单验证
const formRules = reactive<any>({
  cid: [{
    required: true,
    message: '请选择院区',
    trigger: ['blur']
  }],
  name: [{
    required: true,
    message: '请输入名称',
    trigger: ['blur']
  }],
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await apiBuildingDetail({
    id: row.id
  })
  setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
  await formRef.value?.validate()
  const data = {...formData,}
  mode.value == 'edit'
      ? await apiBuildingEdit(data)
      : await apiBuildingAdd(data)
  popupRef.value?.close()
  emit('success')
}

//打开弹窗
const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})

</script>
